package com.fdd.furns.web;

import com.fdd.furns.pojo.Furn;
import com.fdd.furns.pojo.Page;
import com.fdd.furns.service.FurnService;
import com.fdd.furns.service.impl.FurnServiceImpl;
import com.fdd.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = {"/customerFurnServlet"})
public class CustomerFurnServlet extends BasicServlet{
    FurnService furnService = new FurnServiceImpl();

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Furn> page = furnService.page(pageNo, pageSize);
        req.setAttribute("page", page);
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);
    }

    protected void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String name = req.getParameter("name");
        if(null == name){
            name = "";
        }

        Page<Furn> page = furnService.pageByName(pageNo, pageSize, name);
        req.setAttribute("page", page);
        req.setAttribute("name", name);
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);
    }
}
